#include "funcoes.h"



void SocketClient::connectSocket(QString host, int port) {
	blockSize = 0;
	sck->abort();
	sck->connectToHost(host,port);
}

SocketClient::SocketClient() {
	sck = new QTcpSocket();
}


int SocketClient::sendFunction(int idFunction,QStringList Parans) {
	printf("MANDA DO UM %i\n",idFunction);
	Parans.push_front(QString::number(id));
	Parans.push_front(QString::number(idFunction));
	QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	out.setVersion(QDataStream::Qt_4_0);
	out << Parans;
	out.device()->seek(0);
	sck->write(block);
	sck->flush();
	return 1;
}

void SocketClient::requestWellList() {
	QStringList Param;
	sendFunction(GET_WELL_LIST,Param);	
}

void SocketClient::loginRequest(QString login, QString pass) {
	QStringList Param;
	Param.push_back(login);
	Param.push_back(pass);	
	sendFunction(HAND_SHAKE,Param);	
}

void SocketClient::registerWellRequest(QString name, int id, int idMaster) {
	QStringList Param;
	Param.push_back(QString::number(id));
	Param.push_back(name);
	Param.push_back(QString::number(idMaster));
	sendFunction(REGISTER_WELL,Param);
}

void SocketClient::getAlarmsWells() {
	QStringList Param;
	std::map<QString,wells>::iterator begin = wMap.begin();
	for(; begin != wMap.end(); begin++) {
		printf("MANDAR REQUISICAO PARA PEGAR O ESTADO DO POCO %i\n",(*begin).second.id);
		if((*begin).second.getStringState() != "???")
			getAlarmsWell((*begin).second.name);
	}
	
}

void SocketClient::getAlarmsWell(QString wellId) {
	QStringList Param;
	Param.push_back(wellId); //ID do poco
	Param.push_back(QString::number(wMap[wellId].idSimulador)); //Id do mestre
	sendFunction(GET_ALARMS_FROM_WELL,Param);
}

void SocketClient::getStateWells() {
	QStringList Param;
	std::map<QString,wells>::iterator begin = wMap.begin();
	for(; begin != wMap.end(); begin++) {
		printf("MANDAR REQUISICAO PARA PEGAR O ESTADO DO POCO %i\n",(*begin).second.id);
		getStateWell((*begin).second.name);
	}
	
}

void SocketClient::getStateWell(QString wellId) {
	QStringList Param;
	Param.push_back(wellId); //ID do poco
	Param.push_back(QString::number(wMap[wellId].idSimulador)); //Id do mestre
	Param.push_back(QString::number(30001));
	sendFunction(GET_STATE_WELL,Param);
}

void SocketClient::getHistoricFromWell(QString wellId) {
	QStringList Param;
	Param.push_back(wellId); //ID do poco
	sendFunction(GET_HISTORIC_FROM_WELL,Param);
}
